package com.create404.suishoupai.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.create404.suishoupai.dto.LikedPhotoRecordsDto;
import com.create404.suishoupai.dto.PageVo;
import com.create404.suishoupai.entity.PhotoRecordLike;
import com.create404.suishoupai.service.IPhotoRecordLikeService;
import com.create404.suishoupai.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author create404
 * @since 2021-07-17
 */
@Api(tags = "照片记录点赞接口")
@RestController
@RequestMapping("/photo-record-like")
public class PhotoRecordLikeController {

    @Autowired
    IPhotoRecordLikeService iPhotoRecordLikeService;

    @PostMapping("/addPhotoRecordLike")
    public Result addPhotoRecordLike(String photoRecordId, String employeeId) {
        PhotoRecordLike photoRecordLike = new PhotoRecordLike();
        photoRecordLike.setPhotoRecordId(photoRecordId);
        photoRecordLike.setEmployeeId(employeeId);
        photoRecordLike.setCreateTime(LocalDateTime.now());
        photoRecordLike.setUpdateTime(LocalDateTime.now());
        photoRecordLike.setTime(LocalDateTime.now());
        boolean result = iPhotoRecordLikeService.save(photoRecordLike);
        if (result) {
            return Result.ok("点赞成功");
        } else {
            return Result.error("点赞失败");
        }
    }

    @DeleteMapping("/deletePhotoRecordLike")
    public Result deletePhotoRecordLike(String photoRecordId, String employeeId) {

        boolean result = iPhotoRecordLikeService.remove(this.iPhotoRecordLikeService.deletePhotoRecordLiked(photoRecordId, employeeId));
        if (result) {
            return Result.ok("取消点赞成功");
        } else {
            return Result.error("取消点赞成功");
        }
    }

    @GetMapping("/isPhotoRecordLiked")
    public Result isPhotoRecordLiked(String photoRecordId, String employeeId) {
        return Result.ok(this.iPhotoRecordLikeService.isPhotoRecordLiked(photoRecordId, employeeId).size());
    }


    @PostMapping("/getLikedPhotoRecords")
    public Result getLikedPhotoRecords(@RequestBody PageVo pageVo) {
        String employeeId = pageVo.getEmployeeId();
        Integer currentPage = Integer.valueOf(pageVo.getPageNum());
        Integer pageSize = Integer.valueOf(pageVo.getPageSize());
        Page<LikedPhotoRecordsDto> likedPhotoRecordsDtoPage = new Page<LikedPhotoRecordsDto>(currentPage, pageSize);
        IPage<LikedPhotoRecordsDto> page = iPhotoRecordLikeService.getLikedPhotoRecords(likedPhotoRecordsDtoPage, employeeId);
        return Result.ok(page);
    }

}
